﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using EuphoricEngine;

namespace EuphoricEngine_SpaceInvaders_Demo
{
    class HUD : Component
    {
        Texture2D ship; // The player ship texture, for drawing the number of lives remaining
        SpriteFont scoreFont; // The font used to draw text onto the HUD
        Texture2D blank; // A single black pixel used to draw the background of messages over the game

        int score = 0;
        int lives = 3;

        public bool Lost
        {
            get { return lives == 0; }
        }

        public HUD()
        {
        }

        protected override void Load()
        {
            base.Load();

            ship = Parent.Engine.Content.Load<Texture2D>("Content/ship");
            scoreFont = Parent.Engine.Content.Load<SpriteFont>("Content/score_font");

            // Create a new 1x1 texture, with a single black pixel
            blank = new Texture2D(Parent.Engine.GraphicsDevice, 1, 1);
            blank.SetData<Color>(new Color[] { Color.Black });
        }

        public void RegisterAlienHit()
        {
            score += 100;
        }

        public void RegisterPlayerDied()
        {
            lives--;
            score -= 500;
        }

        bool won = false;

        public void RegisterPlayerWon()
        {
            won = true;
        }

        public void RegisterPlayerLost()
        {
            won = false;
            lives = 0;
            score -= 1000;
        }

        public override void Draw()
        {
            base.Draw();

            SpriteBatch b = Parent.Engine.SpriteBatch;

            b.Begin();

            int screenWidth = Parent.Engine.GraphicsDevice.PresentationParameters.BackBufferWidth;
            int screenHeight = Parent.Engine.GraphicsDevice.PresentationParameters.BackBufferHeight;

            // Draw the number of lives left on the top left
            for (int i = 0; i < lives; i++)
                b.Draw(ship, new Rectangle(10 + i * (ship.Width + 10), 10, ship.Width, ship.Height), Color.White);

            // Draw the score on the top right
            Vector2 textSize = scoreFont.MeasureString(score.ToString());
            b.DrawString(scoreFont, score.ToString(), new Vector2(screenWidth - textSize.X - 10, 3), Color.White);

            if (won)
            {
                // Draw "You win!" in the center of the screen
                Vector2 lostSize = scoreFont.MeasureString("You win!");
                b.Draw(blank, new Rectangle(screenWidth / 2 - (int)lostSize.X / 2 - 10, screenHeight / 2 - (int)lostSize.Y / 2, (int)lostSize.X + 20, (int)lostSize.Y), Color.White);
                b.DrawString(scoreFont, "You win!", new Vector2(screenWidth / 2 - lostSize.X / 2, screenHeight / 2 - lostSize.Y / 2), Color.Green);
            }
            else if (Lost)
            {
                // Draw "You lose!" in the center of the screen
                Vector2 lostSize = scoreFont.MeasureString("You lose!");
                b.Draw(blank, new Rectangle(screenWidth / 2 - (int)lostSize.X / 2 - 10, screenHeight / 2 - (int)lostSize.Y / 2, (int)lostSize.X + 20, (int)lostSize.Y), Color.White);
                b.DrawString(scoreFont, "You lose!", new Vector2(screenWidth / 2 - lostSize.X / 2, screenHeight / 2 - lostSize.Y / 2), Color.Red);
            }

            b.End();
        }
    }
}
